package models

import (
	"fmt"
	"time"

	"github.com/BertoldVdb/go-ais"
	geojson "github.com/paulmach/go.geojson"
)

// TrackFeature ShipTrack GeoFeature
type TrackFeature struct {
	MMSI    string
	Feature *geojson.Feature
}

// ToGeoFeature
func ToTrackGeoFeature(r *ais.PositionReport) *TrackFeature {
	mmsi := fmt.Sprintf("%d", r.UserID)
	return &TrackFeature{
		MMSI:    mmsi,
		Feature: ToGeoFeature(mmsi, r),
	}
}

// ToGeoFeature
func ToGeoFeature(mmsi string, r *ais.PositionReport) *geojson.Feature {
	p := geojson.NewPointGeometry([]float64{float64(r.Longitude), float64(r.Latitude)})
	f := geojson.NewFeature(p)

	t := time.Now()

	f.SetProperty("mmsi", mmsi)
	f.SetProperty("cog", r.Cog)
	f.SetProperty("sog", r.Sog)
	f.SetProperty("heading", r.TrueHeading)
	f.SetProperty("rot", r.RateOfTurn)
	f.SetProperty("navStatus", r.NavigationalStatus)
	f.SetProperty("timestamp", t.Unix())
	f.SetProperty("time", t.Local().Format("15:04:05"))
	return f
}

func (r *ShipTrack) ToGeoFeature() *geojson.Feature {
	p := geojson.NewPointGeometry([]float64{float64(r.Lng), float64(r.Lat)})
	f := geojson.NewFeature(p)

	t := time.Now()
	f.SetProperty("channel", r.Channel)
	f.SetProperty("mmsi", r.MMSI)
	f.SetProperty("cog", r.Cog)
	f.SetProperty("sog", r.Sog)
	f.SetProperty("heading", r.Heading)
	f.SetProperty("rot", r.Rot)
	f.SetProperty("navStatus", r.NavStatus)
	f.SetProperty("timestamp", t.Unix())
	f.SetProperty("time", t.Local().Format("15:04:05"))
	return f
}
